/////////////////////////////////////////////////////////////////////////////////////
// TelliTwitter Javascript
// 4/12/2007, JLA
/////////////////////////////////////////////////////////////////////////////////////

var visibleContainer="tlcontainer";

// Utility function to shorten the GEBI call, old Atlas style
function $(elementName)
{
	return document.getElementById(elementName);
}


// Utility function to trim out any spaces on the left or right of a string
function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}


// This function is called onload and handles bootstrapping any load functions
function body_onload()
{

    // Setup the resize
    tlcontainer_resize();

    // Setup the error handler
    window.onerror = handleError;
    
}

function init_directmessage_container()
{
    var mainDiv = $('tlcontainer');
    
    mainDiv.style.overflow = "auto";

    mainDiv = $('ptlcontainer');
    mainDiv.style.overflow = "auto";
}

function handleError() {

    alert('An unhandled error was encountered. Please try again in a few minutes.\n\nIf the problem persists, email us at tellitwitter@gmail.com.');

	return true;
}


// This function is called onload and onresize of the browser control
function tlcontainer_resize()
{
/*
	var mainDiv = $('tlcontainer');


    try
    {
    	var mainDiv = $('tlcontainer');
        
        // If we're minimized or something weird, just exit.
        if ((mainDiv.parentNode.parentNode.offsetHeight - 65) <= 0)
            return;

        // Resize the div according to the parent        
	    mainDiv.style.height = mainDiv.parentNode.parentNode.offsetHeight - 65 + 'px';

        // Resize the other container.
        mainDiv = $('ptlcontainer');

        // Resize the div according to the parent        
	    mainDiv.style.height = mainDiv.parentNode.parentNode.offsetHeight - 65 + 'px';
    }
    catch (error)
    {
        alert('An error was encountered while handling the resize of the window. Please close and re-open the application.\n\nThe error was: ' + error);
        return;
    }*/
}


function setContents(contents, container)
{
 
    var mainDiv = $(container);
    //alert(mainDiv );
    mainDiv.style.display="";
    if(contents.length > 0)
    {
        var newDiv = document.createElement('div');    
        
        newDiv.innerHTML = contents;
        
        if(mainDiv.childNodes.length > 0)
            mainDiv.insertBefore(newDiv, mainDiv.childNodes[0]);
        else
            mainDiv.appendChild(newDiv);
 
    }
    if(visibleContainer!=container) mainDiv.style.display="none";
    setMessage('');
}

function setMessageAndClear(message)
{
    // Set the message
    setMessage(message);
    
    // 10 seconds later, make it go away
    window.setTimeout("setMessage('')", 5000);
}

function setMessage(messageString)
{
    var messageDiv = $('message');
    
    //alert(messageString);
    
    if (messageString.length == 0)
        messageDiv.style.display = 'none';
    else
    {
        messageDiv.innerHTML = messageString;
        messageDiv.style.display = '';
    }

}

function setCurrentUser(markup)
{
    var currentUserDiv = $('currentUser');
    
    alert(markup);
    currentUserDiv.innerHTML = markup;
}

function setTargetUser(markup)
{
    var targetUserDiv = $('targetUser');
    
    targetUserDiv.innerHTML = markup;
}

function displayOptionsDialog()
{
    // Open the options dialog
    window.external.OpenOptionsDialog();
}

function openLinkInDefaultBrowser(link)
{
    window.external.OpenLinkInDefaultBrowser(link);
    return false;
}

function tab_click(o) {
    if(o.id=="tabFriendTimeline")
    {
        show('tlcontainer');hide('ptlcontainer');
        if(o.className.indexOf("tabSelected") < 0) o.className+=" tabSelected";
        $("tabPublicTimeline").className=$("tabPublicTimeline").className.replace(" tabSelected","");
    } else {
        show('ptlcontainer');hide('tlcontainer');
        if(o.className.indexOf("tabSelected") < 0) o.className+=" tabSelected";
        $("tabFriendTimeline").className=$("tabFriendTimeline").className.replace(" tabSelected","");
    }
}

function show(id){ $(id).style.display=""; visibleContainer=id; }
function hide(id){ $(id).style.display="none"; }